-
-
Notifications
You must be signed in to change notification settings - Fork 197
feat(): Add trailing newline when saving package.json #4072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(): Add trailing newline when saving package.json #4072
Conversation
lib/common/file-system.ts
Outdated
@@ -205,7 +207,13 @@ export class FileSystem implements IFileSystem { | |||
space = this.getIndentationCharacter(filename); | |||
} | |||
|
|||
return this.writeFile(filename, JSON.stringify(data, null, space), encoding); | |||
let stringifiedData = JSON.stringify(data, null, space); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be moved inside an else
statement. No need to stringify data multiple times.
let stringifiedData;
if (path.basename(filename) === "package.json") {
const newline = detectNewline(stringifiedData);
stringifiedData = stringifyPackage(data, space, newline);
} else {
stringifiedData = JSON.stringify(data, null, space);
}
lib/common/file-system.ts
Outdated
@@ -205,7 +207,13 @@ export class FileSystem implements IFileSystem { | |||
space = this.getIndentationCharacter(filename); | |||
} | |||
|
|||
return this.writeFile(filename, JSON.stringify(data, null, space), encoding); | |||
let stringifiedData = JSON.stringify(data, null, space); | |||
if (path.basename(filename) === "package.json") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package.json
might be used from constants
:
import { PACKAGE_JSON_FILE_NAME } from "../constants";
if (path.basename(filename) === PACKAGE_JSON_FILE_NAME ) {
...
}
package.json
Outdated
@@ -37,6 +37,7 @@ | |||
"cli-table": "https://github.com/telerik/cli-table/tarball/v0.3.1.2", | |||
"color": "3.0.0", | |||
"colors": "1.1.2", | |||
"detect-newline": "^2.1.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is safer to use an exact version. You can use npm i detect-newline --save-exact
. Same goes for stringify-package
dependency.
Hi @Logikgate , Congratulations on being one of the winners in the {N} First-time contributors contest! You can claim your prize by contacting us at nativescriptwinners[at]progress.com not later than Nov 30th 2018 . Make sure you send us the following info: Best regards, |
PR Checklist
What is the current behavior?
Nativescript-CLI commands that save package.json are stripping the trailing newline that NPM inserts when saving package.json. This can cause un-needed commits of package.json where the only thing that has changed is the removal of the trailing newline.
What is the new behavior?
This pull request uses the module stringify-package, the same one used by NPM, to stringify package.json so that the formatting is kept the same. The method writeJSON now uses stringify-package only if it is saving a file named package.json.
Fixes/Implements/Closes #4049 .